{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# default_exp wandb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Weights & Biases Sweeps\n",
    "\n",
    "> Weights & Biases Sweeps are used to automate hyperparameter optimization and explore the space of possible models."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "from fastcore.script import *\n",
    "from tsai.imports import *\n",
    "from tsai.utils import *\n",
    "from tsai.export import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # hide\n",
    "# import os\n",
    "# from tsai.imports import *\n",
    "# from tsai.utils import *\n",
    "# from fastcore.script import *\n",
    "# from fastcore.xtras import *\n",
    "\n",
    "# @call_parse\n",
    "# def run_sweep(\n",
    "#     sweep:     Param(\"Path to YAML file with the sweep config\", str) = None,\n",
    "#     program:   Param(\"Path to Python training script\", str) = None,\n",
    "#     launch:    Param(\"Launch wanbd agent.\", store_false) = True,\n",
    "#     count:     Param(\"Number of runs to execute\", int) = None,\n",
    "#     entity:    Param(\"username or team name where you're sending runs\", str) = None,\n",
    "#     project:   Param(\"The name of the project where you're sending the new run.\", str) = None,\n",
    "#     sweep_id:  Param(\"Sweep ID. This option omits `sweep`\", str) = None,\n",
    "#     relogin:   Param(\"Relogin to wandb.\", store_true) = False,\n",
    "#     login_key: Param(\"Login key for wandb\", str) = None,\n",
    "# ):\n",
    "\n",
    "#     # import wandb\n",
    "#     try:\n",
    "#         import wandb\n",
    "#     except ImportError:\n",
    "#         raise ImportError('You need to install wandb to run sweeps!')\n",
    "\n",
    "#     # Login to W&B\n",
    "#     if relogin:\n",
    "#         wandb.login(relogin=True)\n",
    "#     elif login_key:\n",
    "#         wandb.login(key=login_key)\n",
    "\n",
    "#     # Sweep id\n",
    "#     if not sweep_id:\n",
    "#         # Load the sweep config\n",
    "#         assert os.path.isfile(sweep), f\"can't find file {sweep}\"\n",
    "#         if isinstance(sweep, str):\n",
    "#             sweep = yaml2dict(sweep)\n",
    "#         if program is None:\n",
    "#             program = sweep[\"program\"]\n",
    "#         # Initialize the sweep\n",
    "#         print('Initializing sweep...')\n",
    "#         sweep_id = wandb.sweep(sweep=sweep, entity=entity, project=project)\n",
    "#         print('...sweep initialized')\n",
    "\n",
    "#     # Load your training script\n",
    "#     print('Loading training script...')\n",
    "#     assert program is not None, \"you need to pass either a sweep or program path\"\n",
    "#     while True: \n",
    "#         if program[0] in \"/ .\": program = program.split(program[0], 1)[1]\n",
    "#         else: break\n",
    "#     if '/' in program and program.rsplit('/', 1)[0] not in sys.path: sys.path.append(program.rsplit('/', 1)[0])\n",
    "# #     assert os.path.isfile(program), f\"can't find file program = {program}\"\n",
    "#     train_script, file_path = import_file_as_module(program, True)\n",
    "#     assert hasattr(train_script, \"train\")\n",
    "#     train_fn = train_script.train\n",
    "#     print('...training script loaded')\n",
    "\n",
    "#     # Launch agent\n",
    "#     if launch:\n",
    "#         print('\\nRun additional sweep agents with:\\n')\n",
    "#     else:\n",
    "#         print('\\nRun sweep agent with:\\n')\n",
    "#     print('    from a notebook:')\n",
    "#     print('        import wandb')\n",
    "#     print(f'        from {file_path} import train')\n",
    "#     print(f\"        wandb.agent('{sweep_id}', function=train, count=None)\\n\")\n",
    "#     print('    from a terminal:')\n",
    "#     print(\n",
    "#         f\"        wandb agent {os.environ['WANDB_ENTITY']}/{os.environ['WANDB_PROJECT']}/{sweep_id}\\n\")\n",
    "#     if launch:\n",
    "#         print('Running agent...')\n",
    "#         try: \n",
    "#             wandb.agent(sweep_id, function=train_fn, count=count)\n",
    "#         except KeyboardInterrupt:\n",
    "#             pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# hide\n",
    "# @call_parse\n",
    "# def run_sweep(path:      Param('Path to file with the sweep script file', str) = None,\n",
    "#               entity:    Param(\"username or team name where you're sending runs\", str) = None,\n",
    "#               project:   Param(\"The name of the project where you're sending the new run.\", str) = None,\n",
    "#               count:     Param('Number of runs to execute', int) = None,\n",
    "#               sweep_id:  Param('Sweep ID. This option omits `sweep`', str) = None,\n",
    "#               launch:    Param(\"Launch wanbd agent.\", store_false) = True,\n",
    "#               relogin:   Param('Relogin to wandb.', store_true) = False,\n",
    "#               login_key: Param('Login key for wandb', str) = None,\n",
    "#               ):\n",
    "\n",
    "#     try: import wandb\n",
    "#     except ImportError: raise ImportError('You need to install wandb to run sweeps!')\n",
    "        \n",
    "#     # Login to W&B\n",
    "#     if relogin: wandb.login(relogin=True)\n",
    "#     elif login_key: wandb.login(key=login_key)\n",
    "        \n",
    "#     # Sweep id\n",
    "#     mod = import_file_as_module(path)\n",
    "#     if not sweep_id:\n",
    "#         sweep_id = wandb.sweep(mod.sweep, entity=entity, project=project)\n",
    "    \n",
    "#     # Agent\n",
    "#     print(f\"\\nwandb agent {os.environ['WANDB_ENTITY']}/{os.environ['WANDB_PROJECT']}/{sweep_id}\\n\")\n",
    "#     if launch: wandb.agent(sweep_id, function=mod.train, count=count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# export\n",
    "def wandb_agent(script_path, sweep, entity=None, project=None, count=None, run=True):\n",
    "    try: import wandb\n",
    "    except ImportError: raise ImportError('You need to install wandb to run sweeps!')\n",
    "    if 'program' not in sweep.keys(): sweep[\"program\"] = script_path\n",
    "    sweep_id = wandb.sweep(sweep, entity=entity, project=project)\n",
    "    entity = ifnone(entity, os.environ['WANDB_ENTITY'])\n",
    "    project = ifnone(project, os.environ['WANDB_PROJECT'])\n",
    "    print(f\"\\nwandb agent {entity}/{project}/{sweep_id}\\n\")\n",
    "    if run: wandb.agent(sweep_id, function=None, count=count)\n",
    "\n",
    "get_wandb_agent = named_partial(\"get_wandb_agent\", wandb_agent, run=False)\n",
    "\n",
    "run_wandb_agent = named_partial(\"run_wandb_agent\", wandb_agent, run=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src onerror=\"\n",
       "        this.nextElementSibling.focus();\n",
       "        this.dispatchEvent(new KeyboardEvent('keydown', {key:'s', keyCode: 83, metaKey: true}));\n",
       "        \" style=\"display:none\"><input style=\"width:0;height:0;border:0\">"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "201_wandb.ipynb saved at 2021-09-29 13:28:16.\n",
      "Converted 201_wandb.ipynb.\n",
      "\n",
      "\n",
      "Correct conversion! 😃\n",
      "Total time elapsed 0.128 s\n",
      "Wednesday 29/09/21 13:28:17 CEST\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "                <audio  controls=\"controls\" autoplay=\"autoplay\">\n",
       "                    <source src=\"data:audio/wav;base64,UklGRvQHAABXQVZFZm10IBAAAAABAAEAECcAACBOAAACABAAZGF0YdAHAAAAAPF/iPh/gOoOon6w6ayCoR2ZeyfbjobxK+F2Hs0XjKc5i3DGvzaTlEaraE+zz5uLUl9f46fHpWJdxVSrnfmw8mYEScqUP70cb0Q8X41uysJ1si6Eh1jYzXp9IE2DzOYsftYRyoCY9dJ/8QICgIcEun8D9PmAaBPlfT7lq4MFIlh61tYPiCswIHX+yBaOqT1QbuW7qpVQSv9lu6+xnvRVSlyopAypbGBTUdSalrSTaUBFYpInwUpxOzhti5TOdndyKhCGrdwAfBUcXIJB69p+Vw1egB76+n9q/h6ADglbf4LvnIHfF/981ODThF4m8HiS0riJVjQ6c+/EOZCYQfJrGrhBmPVNMmNArLKhQlkXWYqhbaxXY8ZNHphLuBJsZUEckCTFVHMgNKGJytIDeSUmw4QN4Qx9pReTgb3vYX/TCBuApf75f+P5Y4CRDdN+B+tngk8c8nt03CKGqipgd13OhotwOC5x9MCAknFFcmlmtPmagFFFYOC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7yq6GGMTw==\" type=\"audio/wav\" />\n",
       "                    Your browser does not support the audio element.\n",
       "                </audio>\n",
       "              "
      ],
      "text/plain": [
       "<IPython.lib.display.Audio object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#hide\n",
    "from tsai.imports import create_scripts\n",
    "from tsai.export import get_nb_name\n",
    "nb_name = get_nb_name()\n",
    "create_scripts(nb_name);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
